package com.applabs.dnd.ui;

import java.util.ArrayList;

import com.applabs.dnd.core.Constant;
import com.applabs.dnd.core.DNDMeProfileManager;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class AddEditProfile extends Activity{

//constants...
	private final int CALL = 1;
	private final int MSG = 2;
	private final int CALLBEHAVIOUR = 3;
	private final int MSGBEHAVIOUR = 4;

	int type;
	Button btnSave;
	Intent intent;
	int intentMode;
	TextView txtAllowMsg;
	TextView txtAllowMsgValue;
	TextView txtAllowCall;
	TextView txtAllowCallValue;
	TextView txtDNDCallBehaviour;
	TextView txtDNDCallBehaviourValue;
	TextView txtDNDMsgBehaviour;
	TextView txtDNDMsgBehaviourValue;
	EditText etProfileName;
	
	int callRule;
	int msgRule;
	int callRejectType;
	int msgRejectType;
	
	private DNDMeProfileManager pManager;
	String pName;


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
		intent = getIntent();
		intentMode = intent.getIntExtra("Type", -1);
		pManager = new DNDMeProfileManager(getApplicationContext());

		setContentView(com.applabs.dnd.R.layout.add_edit_profile);
		btnSave = (Button)findViewById(com.applabs.dnd.R.id.save);
		btnSave.setOnClickListener(btnSaveListner);
		
		txtAllowMsg = (TextView)findViewById(com.applabs.dnd.R.id.AllowMsg);
		txtAllowMsg.setOnClickListener(txtAllowMsgListner);
		txtAllowMsgValue = (TextView)findViewById(com.applabs.dnd.R.id.AllowMsgValue);

		txtAllowCall = (TextView)findViewById(com.applabs.dnd.R.id.AllowCall);
		txtAllowCall.setOnClickListener(txtAllowCallListner);
		txtAllowCallValue = (TextView)findViewById(com.applabs.dnd.R.id.AllowCallValue);

		txtDNDCallBehaviour = (TextView)findViewById(com.applabs.dnd.R.id.CallBehaviour);
		txtDNDCallBehaviour.setOnClickListener(txtDNDCallBehaviourListner);
		txtDNDCallBehaviourValue = (TextView)findViewById(com.applabs.dnd.R.id.CallBehaviourType);

		txtDNDMsgBehaviour = (TextView)findViewById(com.applabs.dnd.R.id.MsgBehaviour);
		txtDNDMsgBehaviour.setOnClickListener(txtDNDMsgBehaviourListner);
		txtDNDMsgBehaviourValue = (TextView)findViewById(com.applabs.dnd.R.id.MsgBehaviourType);

		etProfileName = (EditText)findViewById(com.applabs.dnd.R.id.ProfileName);
		
		if(intentMode == Constant.EDIT)
		{
			pName = intent.getStringExtra("Name");
    		//DNDMeProfile tempProfile;
	    	if( null != (pManager.getProfile(pName)) )
	    	{
	    		etProfileName.setText(pName);
	    	}

		}
		
	}

	OnClickListener txtAllowMsgListner = new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			type = MSG;
			showRule();
			
		}
	};

	OnClickListener txtAllowCallListner = new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			type = CALL;
			showRule();
			
		}
	};

	OnClickListener txtDNDCallBehaviourListner = new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			type = CALLBEHAVIOUR;
			showRule();
			
		}
	};
	
	OnClickListener txtDNDMsgBehaviourListner = new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			type = MSGBEHAVIOUR;
			showRule();
			
		}
	};

	OnClickListener btnSaveListner = new View.OnClickListener() {
		
		@Override
		public void onClick(View v) {
			String profileName = etProfileName.getText().toString();
			if(profileName.isEmpty())
			{
				Toast.makeText(getApplicationContext(), "Enter the Profile Name", Toast.LENGTH_SHORT).show();
			} else
			{
				if(intentMode == Constant.ADD)
				{
					intent.putExtra("type", Constant.ADD);
					intent.putExtra("status", 1);
					intent.putExtra("profile", profileName);
				}else
				{
					intent.putExtra("type", Constant.EDIT);
					intent.putExtra("oldProfileName", pName);
				}
				intent.putExtra("status", 1);
				intent.putExtra("profile", profileName);
				intent.putExtra("callRule", callRule);
				intent.putExtra("msgRule", msgRule);
				intent.putExtra("callRejectType", callRejectType);
				intent.putExtra("msgRejectType", msgRejectType);
				setResult(RESULT_OK,intent);
				finish();
			}
		}
	};
	
	void showRule()
	{
    	Intent newIntent = new Intent(this, Rule.class);
    	newIntent.putExtra("Type", type);
    	startActivityForResult(newIntent, type);
	}
	
    @Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if(resultCode == RESULT_OK && requestCode == CALL)
		{
			ArrayList<String> value = data.getStringArrayListExtra("selected");
			txtAllowCallValue.setText(value.toString());
			callRule = getCallRule(value);
		}
		if(resultCode == RESULT_OK && requestCode == MSG)
		{
			
			ArrayList<String> value = data.getStringArrayListExtra("selected");
			txtAllowMsgValue.setText(value.toString());
			msgRule = getMsgRule(value);
		}
		if(resultCode == RESULT_OK && requestCode == CALLBEHAVIOUR)
		{
			
			ArrayList<String> value = data.getStringArrayListExtra("selected");
			txtDNDCallBehaviourValue.setText(value.toString());
			callRejectType = getCallRejectType(value);
		}
		if(resultCode == RESULT_OK && requestCode == MSGBEHAVIOUR)
		{
			ArrayList<String> value = data.getStringArrayListExtra("selected");
			txtDNDMsgBehaviourValue.setText(value.toString());
			msgRejectType = getMsgRejectType(value);	
		}
	}
    
    private int getCallRule(ArrayList<String> list)
    {
    	int flag = 0;
    	//for()
    	return flag;
    }

    private int getMsgRule(ArrayList<String> list)
    {
    	int flag = 0;
    	//for()
    	return flag;
    }

    private int getCallRejectType(ArrayList<String> list)
    {
    	int flag = 0;
    	//for()
    	return flag;
    }

    private int getMsgRejectType(ArrayList<String> list)
    {
    	int flag = 0;
    	//for()
    	return flag;
    }

	private static final String[] CallRuleList = new String[]{
        "Default", "Meeting", "Sleeping", "Gaming", 
	};

	
}
